<template>
  <div class="app-container">
    <div>
      <el-form>
        <el-form-item>
          <label>离职者</label>
            <el-cascader
              style="width:60%"
              placeholder="请选择离职用户"
              v-model="createUser"
              :options="userList"
              :props="{value:'depId',label:'depName',children:'userList'}"
              collapse-tags
              clearable
              filterable
            ></el-cascader>
        </el-form-item>
        <el-form-item>
          <label>交接者</label>
            <el-cascader
              style="width:60%"
              placeholder="请选择交接用户"
              v-model="updateUser"
              :options="userList"
              :props="{value:'depId',label:'depName',children:'userList'}"
              collapse-tags
              filterable
              clearable
            ></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updateUserId(createUser,updateUser)">确认交接</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
  import $ from "@/api/assets";
  import qs from "querystring";

  export default {
    data() {
      return {

        createUser:[],
        updateUser:[],

        findState: false,
        userValue: [],
        dialogTableVisible: false,
        activeName: "1",
        value: "",
        state: 0,
        list: [],
        val: "",
        listLoading: true,
        allLoading: true,
        currentPage: 1,
        pageSize: 10,
        total: 0,
        orgType: "",
        orgRemark: "",
        bizListId: "",
        bizListName: "",
        coopListId: "",
        coopListName: "",
        cities: [],
        options: [],
        assetAttrList: [],
        provinceList: [],
        sourceList: [],
        bizTypeList: [],
        orgTypeList: [],
        peerTypeList: [],
        peerBizList: [],
        userList: [],
        users: [],
        allName: [],
        multipleSelection: [],
      };
    },
    created() {
      this.fetchData();
      $.findDepartmentUser().then(res => {
        this.userList = res.data;
      });
      $.addInit().then(res => {
        if (res.success) {
          this.sourceList = res.data.source;
          this.provinceList = res.data.province;
          this.assetAttrList = res.data.attr;
          this.bizTypeList = res.data.bizTypeList;
          this.orgTypeList = res.data.orgTypeList;
          this.peerTypeList = res.data.peerTypeList;
          this.peerBizList = res.data.peerBizList;
        }
      });
    },
    methods: {
      updateUserId(createUser,updateUser){
        console.log(createUser)
        console.log(updateUser)
        if (createUser.length==0||updateUser.length==0){
          this.$message({
            message:"交接人员不能为空！",
            type:"error"
          })
          return;
        }else if (createUser[1]==updateUser[1]){
          this.$message({
            message:"人员重复，请重新选择！",
            type:"error"
          })
          return
        }
        $.updateUserId({updateUserId:updateUser[1],createUserId:createUser[1]}).then(res=>{
          this.$message({
            message:"交接成功",
            type:"success"
          })
          this.$router.go(0)
        })
      },

      handleTransfer(id) {
        this.dialogTableVisible = false;
        $.updateHandover({userValue: String(this.userValue), userId: id}).then(
          res => {
            if (res.data == "无数据") {
              this.$message({
                type: "error",
                message: "无数据,转入失败!"
              });
              this.list = [];
              this.$router.go(0)
            } else {
              this.$message({
                type: "success",
                message: "转入成功!"
              });
              this.list = [];
              this.$router.go(0)
            }
          }
        );
      },
      handleSelection(val) {
        this.multipleSelection = val
        for (let resKey of this.multipleSelection) {
          this.$refs.table.toggleRowSelection(resKey, true);
        }
      },
      handleSelectionChange() {
        this.dialogTableVisible = true;
        for (var i = 0; i < this.$refs.table.selection.length; i++) {
          this.userValue.push(this.$refs.table.selection[i].partnerId + "");
        }
        $.TransferList().then(response => {
          this.allName = response.data;
          this.allLoading = false;
        });
      },
      reset() {
        (this.users = []),
          (this.orgType = ""),
          (this.orgRemark = ""),
          (this.bizListId = ""),
          (this.bizListName = ""),
          (this.coopListId = ""),
          (this.coopListName = ""),
          (this.cities = []);
      },
      handleUser() {
        this.list = [];
        $.findUserList({
          users: this.users[1],
          partnerType: 5,
          pageIndex: this.currentPage,
          pageSize: this.pageSize
        }).then(response => {
          (this.orgType = ""),
            (this.orgRemark = ""),
            (this.bizListId = ""),
            (this.bizListName = ""),
            (this.coopListId = ""),
            (this.coopListName = ""),
            (this.cities = []);
          this.list = response.data.list;
          this.total = response.data.total;
          this.listLoading = false;
          this.find()
          // console.log(response.data)
        });
      },
      handleExport() {
        this.pushcities = [];
        for (let i in this.cities) {
          this.pushcities.push(this.cities[i][1]);
        }
        let params = qs.stringify({
          createId: this.users[1],
          partnerType: 5,
          "cities[]": this.pushcities.length > 0 ? this.pushcities : null,
          orgType: this.orgType,
          orgRemark: this.orgRemark,
          bizListId: this.bizListId,
          bizListName: this.bizListName,
          coopListId: this.coopListId,
          coopListName: this.coopListName
        });
        $.exportPeerInfo(params).then(response => {
          let blob = new Blob([response.data], {
            type: "application/vnd.ms-excel"
          });

          let fileName = decodeURI(
            response.headers["content-disposition"].split("=")[1]
          );
          if (window.navigator.msSaveOrOpenBlob) {
            navigator.msSaveBlob(blob, fileName);
          } else {
            var link = document.createElement("a");
            link.href = window.URL.createObjectURL(blob);
            link.download = fileName;
            link.click();
            //释放内存
            window.URL.revokeObjectURL(link.href);
          }
        })
      },
      find() {
        this.listLoading = true;
        this.list = [];
        // console.log(this.overArea)
        this.pushcities = [];
        for (let i in this.cities) {
          this.pushcities.push(this.cities[i][1]);
        }
        let params = qs.stringify({
          createId: this.users[1],
          partnerType: 5,
          "cities[]": this.pushcities.length > 0 ? this.pushcities : null,
          orgType: this.orgType,
          orgRemark: this.orgRemark,
          bizListId: this.bizListId,
          bizListName: this.bizListName,
          coopListId: this.coopListId,
          coopListName: this.coopListName,
          pageSize: this.pageSize,
          pageIndex: this.currentPage
        });
        // console.log(params);
        $.findPeerInfo(params).then(response => {
          // console.log(response.data);
          this.list = response.data.list;
          this.total = response.data.total;
          this.listLoading = false;
          this.findState = true
          // console.log(response.data.list);
        });
      },

      fetchData() {
        this.listLoading = true;
        $.findByNameTelHide({
          partnerType: 5,
          pageIndex: this.currentPage,
          pageSize: this.pageSize
        }).then(response => {
          // console.log(response.data);
          this.list = response.data.list;
          this.total = response.data.total;
          this.listLoading = false;
          // console.log(response.data.list);
        });
      },
      handleSizeChange(val) {
        this.pageSize = val;
        this.currentPage = 1;
        // console.log(this.currentPage)
        if (this.state == 1) {
          this.find();
          //console.log(this.currentPage)
        } else this.fetchData();
      },
      handleCurrentChange(val) {
        this.currentPage = val;
        //console.log(val)
        if (this.state == 1) {
          this.find();
        } else {
          // console.log("执行了")
          if (this.findState == true) {
            this.find()
          } else {
            this.fetchData();
          }
        }
      },
      handleEdit(id) {
        const {href} = this.$router.resolve({
          path: "edit",
          query: {id: id}
        });
        window.open(href, '_blank')
      },
      handleDel(id) {
        this.$confirm("是否恢复?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            // console.log(id);
            $.reductions({partnerId: id}).then(response => {
              // console.log(response)
              this.$message({
                type: "success",
                message: "已恢复!"
              });
              this.fetchData();
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "不恢复"
            });
          });
      }
    }
  };
</script>
